package com.yyge.web.servlet;

import com.alibaba.fastjson2.JSONObject;
import com.yyge.dto.GoodsInfoDto;
import com.yyge.util.ServletUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;


@WebServlet("/req")
public class GetRequestServlet extends HttpServlet {
    private static Logger logger = LoggerFactory.getLogger(GetRequestServlet.class);
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
      JSONObject jsonObject = ServletUtil.parameter2jsonObject(req);
        logger.debug("接收到客户端提交的参数 {}", jsonObject);

        // 从request请求中提取url参数，并将url参数转换为GoodsInfoDto类的对象
        // 注意： 必须保证url参数名和GoodsInfoDto类中的属性名要一致
        GoodsInfoDto goodsInfoDto = ServletUtil.parameter2dto(req, GoodsInfoDto.class);
        logger.debug("将url参数转化为GoodsInfoDto的对象 {}", goodsInfoDto);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      logger.debug("reg servlet接收到客户端发起Post请求");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        //一个迭代器
        Enumeration<String> parameterNames= req.getParameterNames();
        while (parameterNames.hasMoreElements()){
            //参数
         String parameter=parameterNames.nextElement();
         String value=req.getParameter(parameter);

         logger.debug("接收客户端发起的post请求， 参数 {}, 值 {}", parameter, value);
        }
    }
}
